package com.example.chapterspringbootandmybatis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chapterspringbootandmybatis.dao.BookMapper;
import com.example.chapterspringbootandmybatis.entity.Book;
import com.example.chapterspringbootandmybatis.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Autowired
    BookMapper bookMapper;

    @Override
    public List<Book> getBySelect(Book book) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        if(book.getAuthor()!="" && book.getAuthor()!=null){
//            queryWrapper.like("author",book.getAuthor());
//        }
//        if(book.getName()!="" && book.getName()!=null){
//            queryWrapper.like("name",book.getName());
//        }
//        if(book.getCategory()!="" && book.getCategory()!=null){
//            queryWrapper.like("category",book.getCategory());
//        }

        LambdaQueryWrapper<Book> queryWrapper =new LambdaQueryWrapper<>();
        if(book.getAuthor()!="" && book.getAuthor()!=null){
             queryWrapper.like(Book::getAuthor,book.getAuthor());
         }
        if(book.getName()!=null &&book.getName()!="" ){
            queryWrapper.like(Book::getName,book.getName());
        }
        if(book.getCategory()!="" && book.getCategory()!=null){
            queryWrapper.like(Book::getCategory,book.getCategory());
        }
        return bookMapper.selectList(queryWrapper);
    }

    @Override
    public IPage getPage(Integer pageNum) {
        IPage page = new Page(pageNum,3);
        page = bookMapper.selectPage(page,null);
        return page;
    }
}
